package com.rd.sys.action.analysis;

import org.springframework.beans.factory.annotation.Autowired;

import baseproj.common.exception.BaseServiceException;

import com.rd.sys.action.BaseAction;
import com.rd.sys.dto.commmon.base.ProtalAjaxResult;
import com.rd.sys.dto.sys.user.UserInfoDto;
import com.rd.sys.service.sys.user.UserInfoService;
import com.rd.sys.service.sys.user.UserLoginService;

public class UserAction extends BaseAction {
    private static final long serialVersionUID = 2598915856478905725L;

    // -------------------------------------以下是数据成员定义-------------------------------------------
    @Autowired
    private UserInfoService userService;
    @Autowired
    private UserLoginService userLoginService;

    private UserInfoDto user;

    // ------------------------------------------以下是业务方法------------------------------------------

    /**
     * 修改用户基本信息
     * 
     * @throws Exception
     */
    public void modifyInfo() throws Exception {
        ProtalAjaxResult result = null;
        try {
            result = ProtalAjaxResult.buildSuccessResult();
        } catch (BaseServiceException ex) {
            result = ProtalAjaxResult.buildFaildResult();
            result.setDescription(ex.getErroDesc());
        } catch (Exception ex) {
            result = ProtalAjaxResult.buildFaildResult();
        } finally {
            ajaxMessage(result);
        }
    }

    /**
     * 修改用户密码
     * 
     * @throws Exception
     */
    public void modifyPwd() throws Exception {
        UserInfoDto sessionUser = this.getUser(request);
        ProtalAjaxResult result = null;
        try {
            user.setUserno(sessionUser.getUserno());
            result = ProtalAjaxResult.buildSuccessResult();
        } catch (BaseServiceException ex) {
            result = ProtalAjaxResult.buildFaildResult();
            result.setDescription(ex.getErroDesc());
        } catch (Exception ex) {
            result = ProtalAjaxResult.buildFaildResult();
        } finally {
            ajaxMessage(result);
        }
    }

    /**
     * 过滤待修改的用户基本信息(未变更的不用提交修改)
     */
    private void filterUserInfo() {
        /*
         * if (StringUtils.equals(sessionUser.getNickname(),
         * user.getNickname())) { user.setNickname(null); } if
         * (StringUtils.equals(sessionUser.getName(), user.getName())) {
         * user.setName(null); } if
         * (StringUtils.equals(sessionUser.getTelephone(), user.getTelephone()))
         * { user.setTelephone(null); } if
         * (StringUtils.equals(sessionUser.getEmail(), user.getEmail())) {
         * user.setEmail(null); }
         */
    }

    /**
     * 更新Session中的用户信息 *
     * 
     * @param isUpdateUserName
     *            是否更新Session登录账号信息
     */
    private void updateSessionUserInfo(boolean isUpdateUserName) {
        UserInfoDto sessionUser = this.getUser(request);
        sessionUser.setNickname(user.getNickname());
        sessionUser.setTelephone(user.getTelephone());
        sessionUser.setEmail(user.getEmail());

        if (isUpdateUserName) {
            // 只有当更新了登录账号时,才更新Session中的登录账号信息
            sessionUser.setName(user.getName());
        }
    }

    // ------------------------------------------以下是get/set方法-----------------------------------------
    public UserInfoDto getUser() {
        return user;
    }

    public void setUser(UserInfoDto user) {
        this.user = user;
    }

}
